/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package network.packets;

import datamodel.DriverState;
import utils.StringUtils;
import utils.Utils;

/**
 *
 * @author Kvest
 */
public class LoginResponcePacket extends Packet
{
    public static final int RESULT_OK = 0;
    public static final int RESULT_WRONG_AUTH_DATA = 1;
    public static final int RESULT_OTHER_ERROR = 2;
    
    private int result;
    private int driverID;
    private DriverState driverState;
    private DriverState driverNextState;
    private int districtID;
    private int currentOrderID;
    private String currentOrderText;
    private String driversOrder;
    private String actualClientVersion;
    private String actualClientLink;
    private boolean mustUpdate;
    private int functionsMask;
    
    public LoginResponcePacket(byte[] data)
    {
        super(LOGIN_RESPONCE, data);
    }

    protected void parse(byte[] data) 
    {
        int offset = 2;
     
        //Успешность залогинивания
        byte[] buffer = new byte[4];
        System.arraycopy(data, offset, buffer, 0, buffer.length);
        offset += buffer.length;
        result = Utils.byteToInt(buffer);
        
        if (result != RESULT_OK)
        {
            return;
        }
        
        //ИД пользователя в системе
        System.arraycopy(data, offset, buffer, 0, buffer.length);
        offset += buffer.length;
        driverID = Utils.byteToInt(buffer);
              
        //состояние водителя 
        buffer = new byte[2];
        System.arraycopy(data, offset, buffer, 0, buffer.length);
        offset += buffer.length;
        int stateId = Utils.byteToInt(buffer);
        
        buffer = new byte[4];
        System.arraycopy(data, offset, buffer, 0, buffer.length);
        offset += buffer.length;
        int stringSize = Utils.byteToInt(buffer);
        buffer = new byte[stringSize];
        System.arraycopy(data, offset, buffer, 0, buffer.length);
        offset += buffer.length;
        String stateName = StringUtils.bytesToStr(buffer);
        
        driverState = new DriverState(stateId, stateName);
        
        //Следующее состояние водителя
        buffer = new byte[2];
        System.arraycopy(data, offset, buffer, 0, buffer.length);
        offset += buffer.length;
        stateId = Utils.byteToInt(buffer);
        
        buffer = new byte[4];
        System.arraycopy(data, offset, buffer, 0, buffer.length);
        offset += buffer.length;
        stringSize = Utils.byteToInt(buffer);
        buffer = new byte[stringSize];
        System.arraycopy(data, offset, buffer, 0, buffer.length);
        offset += buffer.length;
        stateName = StringUtils.bytesToStr(buffer);
        
        driverNextState = new DriverState(stateId, stateName);
        
        //Ид стоянки 
        buffer = new byte[4];
        System.arraycopy(data, offset, buffer, 0, buffer.length);
        offset += buffer.length;
        districtID = Utils.byteToInt(buffer);
        
        //ИД текущего заказа
        buffer = new byte[4];
        System.arraycopy(data, offset, buffer, 0, buffer.length);
        offset += buffer.length;
        currentOrderID = Utils.byteToInt(buffer);
 
        //Текст текущего заказа
        System.arraycopy(data, offset, buffer, 0, buffer.length);
        offset += buffer.length;
        stringSize = Utils.byteToInt(buffer);
        buffer = new byte[stringSize];
        System.arraycopy(data, offset, buffer, 0, buffer.length);
        offset += buffer.length;
        currentOrderText = StringUtils.bytesToStr(buffer);
        
        //список позывных на стоянке
        buffer = new byte[4];
        System.arraycopy(data, offset, buffer, 0, buffer.length);
        offset += buffer.length;
        stringSize = Utils.byteToInt(buffer);
        buffer = new byte[stringSize];
        System.arraycopy(data, offset, buffer, 0, buffer.length);
        offset += buffer.length;
        driversOrder = StringUtils.bytesToStr(buffer);

        //текущая актуальная версия приложения
        buffer = new byte[4];
        System.arraycopy(data, offset, buffer, 0, buffer.length);
        offset += buffer.length;
        stringSize = Utils.byteToInt(buffer);
        buffer = new byte[stringSize];
        System.arraycopy(data, offset, buffer, 0, buffer.length);
        offset += buffer.length;
        actualClientVersion = StringUtils.bytesToStr(buffer);
        
        //ссылка на текущую актуальную версию
        buffer = new byte[4];
        System.arraycopy(data, offset, buffer, 0, buffer.length);
        offset += buffer.length;
        stringSize = Utils.byteToInt(buffer);
        buffer = new byte[stringSize];
        System.arraycopy(data, offset, buffer, 0, buffer.length);
        offset += buffer.length;
        actualClientLink = StringUtils.bytesToStr(buffer);

        mustUpdate = (data[offset] != 0);
        ++offset;
        
        //Побитовая маска для "Функции" 
        buffer = new byte[2];
        System.arraycopy(data, offset, buffer, 0, buffer.length);
        offset += buffer.length;
        functionsMask  = Utils.byteToInt(buffer);
    }

    public void print() 
    {
        System.out.println("LoginResponcePacket[result = " + result + ", driverID=" + driverID + ", driverState=" + (driverState != null ? driverState.toString() : "?") + 
                           ", driverNextState=" + (driverNextState != null ? driverNextState.toString() : "?") +
                            ", districtID=" + districtID + ", currentOrderID=" + currentOrderID + ", currentOrderText=" + currentOrderText + 
                            ", driversOrder=" + driversOrder + ", actualClientVersion=" + actualClientVersion + ", actualClientLink=" + actualClientLink +  ", mustUpdate=" + mustUpdate + 
                            ", functionsMask=" + functionsMask + "]");
    }

    public int getResult() 
    {
        return result;
    }

    public int getCurrentOrderID() 
    {
        return currentOrderID;
    }

    public String getCurrentOrderText() 
    {
        return currentOrderText;
    }

    public int getDistrictID() 
    {
        return districtID;
    }

    public int getDriverID() 
    {
        return driverID;
    }

    public DriverState getDriverState() 
    {
        return driverState;
    }
    
    public DriverState getDriverNextState() 
    {
        return driverNextState;
    }

    public String getActualClientLink() 
    {
        return actualClientLink;
    }

    public String getActualClientVersion() 
    {
        return actualClientVersion;
    }

    public String getDriversOrder() 
    {
        return driversOrder;
    }

    public boolean isMustUpdate()
    {
        return mustUpdate;
    }

    public int getFunctionsMask() 
    {
        return functionsMask;
    }
}
